﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;

namespace GenericValidation
{
    public class ValidationSettings
    {
        public const string InsertTag = "Insert";
        public const string UpdateTag = "Update";
        public const string DeleteTag = "Delete";
        public const string AlwaysTag = "Always";

        public const string IsRequiredTemplate = "{0} is required.";
        public const string IsLowerThanTemplate = "{0} must be lower than {1}.";
        public const string IsGreaterThanTemplate = "{0} must be greater than {1}.";
        public const string IsLengthLowerThanTemplate = "{0} length must be lower than {1}; current length is {2}.";

        public ResourceManager ResourceManager { get; set; }

        public string ResourceKey { get; set; }

        public string DefaultTemplate { get; set; }

        public void Localize(ResourceManager resourceManager, string resourceKey)
        {
            this.ResourceKey = resourceKey;
            this.ResourceManager = resourceManager;
        }

        //public string GetTemplate(string resourceKey)
        //{
        //    //TODO: throw some exceptions when this does not exist???
        //    var theResult = this.DefaultTemplate;
        //    if (this.ResourceManager != null)
        //    {
        //        theResult = this.ResourceManager.GetString(this.ResourceKey);
        //    }

        //    return theResult;
        //}

        
        static ValidationSettings()
        {
            MemberLocalization = new Dictionary<ResourceManager, string[]>();

            IsRequired = new ValidationSettings()
            {
                DefaultTemplate = ""
            };

            IsGreaterThan = new ValidationSettings()
            {
                DefaultTemplate = ""
            };

            IsLengthBetween = new ValidationSettings()
            {
                DefaultTemplate = ""
            };

            IsLowerThan = new ValidationSettings()
            {
                DefaultTemplate = ""
            };

            IsLengthLowerThan = new ValidationSettings()
            {
                DefaultTemplate = ""
            };
        }

        public static ValidationSettings IsRequired { get; set; }
        public static ValidationSettings IsGreaterThan { get; set; }
        public static ValidationSettings IsLowerThan { get; set; }
        public static ValidationSettings IsLengthBetween { get; set; }
        public static ValidationSettings IsLengthLowerThan { get; set; }

        public static IDictionary<ResourceManager, string[]> MemberLocalization { get; set; }

        public static string GetLocalizedMemberName<T>(string key)
            where T : class
        {
            var theResult = key;
            var query = from p in MemberLocalization
                         where p.Value != null && p.Value.Contains(key)
                         select p.Key;

            var resourceManager = query.Single();
            var resourceValue = default(string);
            if (resourceManager != null && (resourceValue = resourceManager.GetString(key)) != null && string.IsNullOrEmpty(resourceValue) == false)
            {
                theResult = resourceValue;
            }

            return theResult;
        }

        public static void LocalizeMembers(ResourceManager resourceManager, params string[] tags)
        {
            MemberLocalization.Add(resourceManager, tags);
        }

    }
}
